using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using star_questionnair.Models;
using star_questionnair.Models.Enum;

namespace star_questionnair.Services.impl;

public class SortQuestionAnalysisService : ISortQuestionAnalysisService
{
     private readonly IQuestionService _questionService;
     private readonly IHistoryService _historyService;

        public SortQuestionAnalysisService(IQuestionService questionService, IHistoryService historyService)
        {
            _questionService = questionService;
            _historyService = historyService;
        }

        public async Task<Dictionary<string, double>> GetSortOptionScoreAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为排序题类型
            if (!QuestionEnumExtensions.IsSortType(question.Type))
            {
                return null;
            }

            // 获取该问题的所有选项
            var options =  _historyService.GetOptionsByQuestionId(questionId);

            // 初始化选项得分的字典
            var optionScoreMap = new Dictionary<string, double>();

            // 获取该问题的所有答案
            var answers =  _historyService.GetAnswersByQuestionId(questionId);

            // 计算每个选项的得分
            foreach (var option in options)
            {
                int totalFrequency = 0;
                int totalScore = 0;

                foreach (var answer in answers)
                {
                    if (int.Parse(answer.Content.Trim()) == option.Id)
                    {
                        int position = answer.Sort;  // 排名
                        int weight = options.Count + 1 - position;  // 权值计算
                        totalFrequency++;
                        totalScore += weight;
                    }
                }

                double averageScore = totalFrequency == 0 ? 0.0 : (double)totalScore / totalFrequency;
                optionScoreMap[option.Content] = averageScore;
            }

            return optionScoreMap;
        }

        public async Task<Dictionary<string, List<double>>> GetSortOptionRankingProportionsAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为排序题类型
            if (!QuestionEnumExtensions.IsSortType(question.Type))
            {
                return null;
            }

            // 获取该问题的所有选项
            var options =  _historyService.GetOptionsByQuestionId(questionId);

            // 初始化选项排名占比的字典
            var optionRankingProportions = new Dictionary<string, List<double>>();

            // 获取该问题的所有答案
            var answers =  _historyService.GetAnswersByQuestionId(questionId);

            // 计算每个选项在每个排名中的占比
            foreach (var option in options)
            {
                int[] rankCounts = new int[options.Count]; // 用于存储各个排名的次数
                int totalRanks = 0; // 用于存储总的排名次数

                foreach (var answer in answers)
                {
                    if (int.Parse(answer.Content.Trim()) == option.Id)
                    {
                        int rank = answer.Sort - 1;
                        rankCounts[rank]++;
                        totalRanks++;
                    }
                }

                var rankProportions = rankCounts.Select(count => totalRanks == 0 ? 0.0 : (double)count / totalRanks).ToList();
                optionRankingProportions[option.Content] = rankProportions;
            }

            return optionRankingProportions;
        }
}